<template>
    <div class="update-birthday">
        <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="修改生日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="onConfirm" />
    </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
import dayjs from 'dayjs'

export default {
    name: 'UpdateBirthday',
    model: {
        prop: 'birthday',
        event: 'updateBirthday'
    },
    props: ['birthday'],
    data () {
        return {
            minDate: new Date(1970, 0, 1),
            maxDate: new Date(),
            currentDate: new Date(this.birthday)
        }
    },
    methods: {
        async onConfirm () {
            this.$toast.loading({
                message: '保存中...',
                forbidClick: true, // 禁止背景点击
                duration: 0 // 持续时间
            })
            try {
                const localbd = dayjs(this.currentDate).format('YYYY-MM-DD')
                await updateUserProfile({
                    birthday: localbd
                })
                this.$emit('updateBirthday', localbd)
                this.$emit('close')
                this.$toast.success('更新成功')
            } catch (error) {
                this.$toast.fail('更新失败')
            }
        }
    }
}
</script>
